package com.lancoo.ccas53.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas53.entity.CssProhibitStudent;
import com.lancoo.ccas53.entity.Student;
import com.lancoo.ccas53.mapper.CssProhibitStudentMapper;
import com.lancoo.ccas53.pojo.common.PageInfo;
import com.lancoo.ccas53.pojo.dto.ProhibitStudentDto;
import com.lancoo.ccas53.pojo.dto.ResultDto;
import com.lancoo.ccas53.service.CssProhibitStudentService;
import com.lancoo.ccas53.service.StudentService;
import com.lancoo.ccas53.util.PagingUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 禁选学生 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Service
public class CssProhibitStudentServiceImpl extends ServiceImpl<CssProhibitStudentMapper, CssProhibitStudent> implements CssProhibitStudentService {
    @Resource
    private StudentService studentService;

    @Override
    public ResultDto<ProhibitStudentDto> getStudentList(Integer type, String baseCollegeId, String gradeId, String classId, String keyword, Integer pageSize, Integer currentPage, Integer taskId) {
        ResultDto<ProhibitStudentDto> result = new ResultDto<>();
        result.setPageSize(pageSize);
        result.setCurrentPage(currentPage);
        int total = 0;
        List<Student> studentList = studentService.lambdaQuery()
                .eq(StringUtils.isNotBlank(baseCollegeId), Student::getBaseCollegeId, baseCollegeId)
                .eq(StringUtils.isNotBlank(gradeId), Student::getGradeId, gradeId)
                .eq(StringUtils.isNotBlank(classId), Student::getClassId, classId)
                .and(StringUtils.isNotBlank(keyword), a -> a.like(Student::getStudentName, keyword).or().like(Student::getBaseStudentId, keyword))
                .eq(Student::getTaskId, taskId)
                .list();
        List<CssProhibitStudent> prohibitStudents = this.lambdaQuery()
                .eq(CssProhibitStudent::getType, type)
                .eq(CssProhibitStudent::getTaskId, taskId)
                .list();

        List<ProhibitStudentDto> list = new ArrayList<>();
        for (CssProhibitStudent item : prohibitStudents
        ) {
            Optional<Student> optional = studentList.stream().filter(a -> a.getStudentId().equals(item.getStudentId())).findFirst();
            if (optional.isPresent()) {
                ProhibitStudentDto view = new ProhibitStudentDto();
                Student student = optional.get();
                BeanUtils.copyProperties(student, view);
                view.setId(item.getId());
                view.setType(item.getType());
                view.setReason(item.getReason());
                list.add(view);
                total += 1;
            }
        }
        PageInfo<ProhibitStudentDto> pageInfo = PagingUtil.getPages(currentPage, pageSize, list);

        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        result.setPages(pageInfo.getPages());
        return result;
    }
}
